<?php
class Zend_Controller_Action_Helper_Fenlei extends Zend_Controller_Action_Helper_Abstract{
	
	static public $flidArray;
	
	public function getCategories(){
		
		$view=$this->getActionController()->view;
		
		$myCache=Zend_Registry::get('myCache');
		
		if(!$result=$myCache->load('tree')){
		
	    	$product=new CFenlei('product');
	    	
	    	$tree=$product->getFenleiTree();
	    	
	    	$view->assign('tree',$tree);
	    	
	    	$flArray=$tree->getChildren(0);
	    	
	    	$registry = Zend_Registry::getInstance();
	    	
	    	$registry->set('tree',$tree);
	    	
	    	$view->assign('flArray',$flArray);

			//Club Soccer Jerseys
			$product->order="en_flname ASC";

			$ClubflArray= $product->getFenleiByPid(92, 0);
			$ClubflArray=array_slice($ClubflArray,0,25);

			$view->assign('ClubflArray',$ClubflArray);
                
	    	/*缓存*/
	    	$myCache->save($tree,'tree');
			
			$myCache->save($ClubflArray,'ClubflArray');
	    	
		    $myCache->save($flArray,'flArray');
		    /*缓存*/
    	
		}else{
			
			$tree=$result;
			
			$view->assign('tree',$tree);
			
			$registry = Zend_Registry::getInstance();
	    	
	    	$registry->set('tree',$tree);
			
			$view->assign('flArray',$myCache->load('flArray'));
			
			$view->assign('ClubflArray',$myCache->load('ClubflArray'));
			
		}
		$this->getNationCategories();
    	
	}
	
	public function getNationCategories(){
		
		$view=$this->getActionController()->view;
		
		$myCache=Zend_Registry::get('myCache');
		
		if(!$result=$myCache->load('nation_tree') || !$result2=$myCache->load('nationFlArray_tree') || !$result3=$myCache->load('nation_flArray')){
		
	    	$product=new CFenlei('product');
	    	
	    	$tree=$product->getFenleiTree();
	    	
	    	$view->assign('tree',$tree);
	    	
	    	$flArray=$tree->getChildren(0);
	    	
	    	$registry = Zend_Registry::getInstance();
	    	
	    	$registry->set('tree',$tree);
	    	
	    	$view->assign('flArray',$flArray);

			  //Club Soccer Jerseys
			$product->order="en_flname ASC";

			$nationFlArray= $product->getFenleiByPid(93);

			$view->assign('nationFlArray',$nationFlArray);
                
	    	/*缓存*/
	    	$myCache->save($tree,'nation_tree');
			
			$myCache->save($nationFlArray,'nationFlArray_tree');
	    	
		    $myCache->save($flArray,'nation_flArray');
		    /*缓存*/
    	
		}else{
			$view->assign('nation_tree',$myCache->load('nation_tree'));
			$view->assign('nation_flArray',$myCache->load('nation_flArray'));
			$view->assign('nationFlArray',$myCache->load('nationFlArray_tree'));

			$registry = Zend_Registry::getInstance();
	    	$registry->set('tree',$myCache->load('nation_tree'));
			
		}
    	
	}
	
    public function getFlname($flArray,$productArray){
    	
    	$fenlei=new CFenlei('product');

    	$flnameArray=$fenlei->getFenLeiNameByFlidArray($flArray,'en');
 
    	$flnameArray2=array();
    	
    	foreach ($flnameArray as $value) {
    		
    		$flnameArray2[$value['flid']]['en_flname']=$value['en_flname'];
    		
    		$flnameArray2[$value['flid']]['counts']=0;
    		
    	}
    	
    	if($productArray['hasresult']=='1'){
    		
	    	foreach($productArray['result'] as $value){
	
	    		if(in_array($value['flid'],$flArray)){
	    			
	    			$flnameArray2[$value['flid']]['counts']+=1;
	    			
	    		}
	    		
	    	}
	    	
    	}
    	
    	return $flnameArray2;
    	
    }
	
	public function getAllParents($view,$flid){
		
		$tree=Zend_Registry::get('tree');
		
		$node=null;

		$parentArray=array();
		
		$data=$tree->getData($flid);
		
		$parentArray[]='<a href="'.MY_URL.'/category-'.$data['flid'].'-0-1-'.$view->encodeName($data['en_flname']).'.html">'.$data['en_flname'].'</a>';
		
		while (true){

			$node=$tree->getParent($flid);

			if($node['id']==0){

				break;
				
			}else{
				$logoStr = "";
				if($node['pid']=='0'){
					$logoStr = "logo";
				} else{

					$tempnode =$tree->getParent($node['id']);
				    if($tempnode['pid']=='0')
					    $logoStr = "logo";
				}
				array_unshift($parentArray,'<a href="'.MY_URL.'/cld-'.$node['data']['flid'].'-'.$view->encodeName($node['data']['en_flname']).'.html">'.$node['data']['en_flname'].'</a>');
				
				$flid=$node['id'];				
			}		
		}
		
		$view->assign('parentArray',$parentArray);
		
		return $parentArray;
			
	}
	
	public function getAllChildren($flid){
		
		$tree=Zend_Registry::get('tree');
		
		$childrenArray=$tree->getChildren($flid);
		
    	return $childrenArray;
		
	}
	
	private function getPid($childArray,$childId){
		
		$tree=Zend_Registry::get('tree');		
	
		while($childId!=''){
			
			if(in_array($childId,$childArray)){

				return $childId;
				
			}else{
				
				$parent=$tree->getParent($childId);
				
				$childId=$parent['data']['flid'];
				
			}
			
		}
		
		return 0;
		
	}
	
	public function getFenleiChild($tree,$flid){
  
		self::$flidArray=array();
		
		$tree->BFS(array($this,'visit'),$flid);
		
		return self::$flidArray;
		
	}
	
	public function visit($node){
		
		if(count($node['child'])==0){
			
			if($node['data']['flid']!=''){	
				
				self::$flidArray[]=$node['data']['flid'];
			
			}
			
		}
			
	}

	
}
